package com.wsm.common.controller;

import com.wsm.common.domain.ShiroPermissionDO;
import com.wsm.common.service.ShiroPermissionService;
import com.wsm.common.utils.PageUtils;
import com.wsm.common.utils.Query;
import com.wsm.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * shiro权限
 *
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-02 20:54:16
 */

@Controller
@RequestMapping("/common/shiroPermission")
public class ShiroPermissionController {
	@Autowired
	private ShiroPermissionService shiroPermissionService;

	@GetMapping()
	@RequiresPermissions("common:shiroPermission:shiroPermission")
	String ShiroPermission() {
		return "common/shiroPermission/shiroPermission";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("common:shiroPermission:shiroPermission")
	public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);
		List<ShiroPermissionDO> shiroPermissionList = shiroPermissionService.list(query);
		int total = shiroPermissionService.count(query);
		PageUtils pageUtils = new PageUtils(shiroPermissionList, total);
		return Result.success(pageUtils);
	}

	@GetMapping("/add")
	@RequiresPermissions("common:shiroPermission:add")
	String add() {
		return "common/shiroPermission/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:shiroPermission:edit")
	String edit(@PathVariable("id") String id, Model model) {
		ShiroPermissionDO shiroPermission = shiroPermissionService.get(id);
		model.addAttribute("shiroPermission", shiroPermission);
		return "common/shiroPermission/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:shiroPermission:add")
	public Result save(ShiroPermissionDO shiroPermission) {
		if (shiroPermissionService.save(shiroPermission) > 0) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:shiroPermission:edit")
	public Result update(ShiroPermissionDO shiroPermission) {
		shiroPermissionService.update(shiroPermission);
		return Result.success();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("common:shiroPermission:remove")
	public Result remove(String id) {
		if (shiroPermissionService.remove(id) > 0) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:shiroPermission:batchRemove")
	public Result remove(@RequestParam("ids[]") String[] ids) {
		shiroPermissionService.batchRemove(ids);
		return Result.success();
	}

	/**
	 * 刷新权限
	 */
	@PostMapping("/reflush")
	@ResponseBody
	@RequiresPermissions("common:shiroPermission:reflush")
	public Result reflush() {
		shiroPermissionService.updatePermission();
		return Result.success();
	}
}
